<?php


namespace app\admin\controller;

use app\common\model\Admins;
use app\common\model\AuthGroup;
use app\common\model\AuthGroupAccess;
use app\common\model\AuthRule;

use think\facade\Config;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
use think\facade\View;
use com\Tree;

class AdminRule extends Base
{
    public $uid = '';
    public function initialize()
    {
        $this->uid = Session::get('admin.admin_id');
        parent::initialize();
    }
    public function index(){
        if(Request::isPost()){
            $uid = $this->uid;

            $arr = Db::name('auth_rule')
                ->order('pid asc,sort asc')
                ->select()->toArray();
            foreach($arr as $k=>$v){
                $arr[$k]['lay_is_open']=false;
            }
            cache('authRuleList_'.$uid, $arr, 3600);

            return $result = ['code'=>0,'msg'=>lang('get info success'),'data'=>$arr,'is'=>true,'tip'=>'操作成功'];
        }

        return View::fetch();
    }

    public function add(){
        if (Request::isPost()){
            $data = Request::post();
            if (AuthRule::create($data)) {
                $this->success('权限节点规则添加成功', 'index');
            } else {
                $this->error('权限节点规则添加失败');
            }
        }else{
            $rulelist = Db::name('auth_rule')
                ->order('sort ASC')
                ->select();
            $rulelist = Tree::cateTree($rulelist);
            $pid = Request::param('id') ? Request::param('id') : 0;
            $view = [
                'pid'=>$pid,
                'info'     => null,
                'rulelist' =>$rulelist,
                'type'=>'添加权限节点规则',
            ];
        }
        View::assign($view);
        return View::fetch();
    }

    public function edit(){
        $id = Request::param('id');
        if(Request::isPost()){
            $data = Request::param();
            $where['id'] = $id;
            if (AuthRule::update($data, $where)){
                $this->success('修改成功!','index');
            }else{
                $this->error('修改失败！');
            }
        }else{
            $info = AuthRule::find($id);
            $rulelist = Db::name('auth_rule')
                ->order('sort ASC')
                ->select();
            $rulelist = Tree::cateTree($rulelist);
            $view = [
                'info' =>$info,
                'rulelist' =>$rulelist,
                'type'=>'编辑权限节点规则',
            ];
        }
        View::assign($view);
        return View::fetch();
    }

    public function ruledel(){
        $rule_id = Request::param('rule_id');
        $childrenids =AdminRule::childrenids($rule_id);
        $childrenids[] = $rule_id;
        if (Db::name('auth_rule')->delete($childrenids)) {
            return success('权限节点规则删除成功', 'index');
        } else {
            return error('权限节点规则删除失败');
        }
    }

    public function childrenids($cateid){
        $data=Db::name('auth_rule')->field('id,pid')->select();
        return $this->_childrenids($data,$cateid);
    }

    private function _childrenids($data,$cateid){
        static $arr=array();
        foreach ($data as $k => $v) {
            if($v['pid']==$cateid){
                $arr[]=$v['id'];
                $this->_childrenids($data,$v['id']);
            }
        }
        return $arr;
    }

    public function sort()
    {
        if (Request::isPost()) {
            $id = Request::param('id');
            $sort = Request::param('sort');
            $info = AuthRule::find($id);
            $info->sort = $sort;
            $info->save();
            $this->success('排序成功', 'index');
        }
    }

    public function open(){
        $id = Request::param('id/d');
        $status = Request::param('status/d');
        if (Db::name('auth_rule')->where(array('id'=>$id))->update(['auth_open' => $status])) {
            $this->success('操作成功！');
        } else {
            return error('操作失败！');
        }
    }

    public function setstate(){
        $id = Request::param('id/d');
        $status = Request::param('status/d');
        if (Db::name('auth_rule')->where(array('id'=>$id))->update(['status' => $status])) {
            $this->success('操作成功！');
        } else {
            return error('操作失败！');
        }
    }

    public function ajaxRule ()
    {
        $key = Request::param('name');
        $value = Request::param('param');
        $id = input('id', 0, 'intval');
        $res = AdminRule::ajaxAdminRule($value, $id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '权限节点名已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }

    /**
     * ajax验证用户名
     */
    public function ajaxAdminRule($title,$id = 0){
        if(empty($title)){
            return 1;
        }
        //组装where条件
        $where = array();
        $where[] = array('title','=',$title);
        if($id > 0){
            $where[] =  array('id','<>',$id);
        }
        return Db::name('auth_rule')->where($where)->find();
    }

    public function ajaxRulename ()
    {
        $key = Request::param('name');
        $value = Request::param('param');
        $id = input('id', 0, 'intval');
        $res = AdminRule::ajaxAdminRulename($value, $id);
        if ($res) {
            $data = array(
                'status' => 'n',
                'info' => '控制器/方法已经重复，请一个试试'
            );
        } else {
            $data = array(
                'status' => 'y',
                'info' => '验证通过'
            );
        }
        return json($data);
    }
    public function ajaxAdminRulename($name,$id = 0){
        if(empty($name)){
            return 1;
        }
        //组装where条件
        $where = array();
        $where[] = array('name','=',$name);
        if($id > 0){
            $where[] =  array('id','<>',$id);
        }
        return Db::name('auth_rule')->where($where)->find();
    }
}